'use strict';
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {
	if (event.leixing == 0) {
		const res = await db.collection('uni-id-users').add({
			name: event.name,
			username:event.tel,
			mobile: event.tel,
			mobile_confirmed:1,
			role: event.role,
			password: "$UNI_ID_HMAC_SHA256$20$4309ffdb6d49c3caf938b6bb234f376235208aa8943de814f8fd83156a99165a13d2d5cb10c4666dd97b",
			password_secret_version:1,
			register_date:event.zhucedate,
			jingshouren: event.jingshouren,	
			status:0,
			yu_e:0,
			dcloud_appid:["__UNI__AE607D4"],
			born_day: event.born_day,
			zhuzhi: event.zhuzhi,
		})
		const res1 = await db.collection('zhangwuliushui').add({
			jine: event.jine,
			biandong: event.biandong,
			leibie: event.leibie,
			detail: event.detail,
			riqi: event.zhucedate,
			jingshouren: event.jingshouren,
			xiaoqu: event.xiaoqu
		})
		const res2 = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.zhucedate,
			jingshouren: event.jingshouren,
			xiaoqu: event.xiaoqu,
			leibie: "添加新学员",
		})
		return res
	} else if (event.leixing == 1) {
		const res = await db.collection('baokelist').add({
			xufeifrom: "",
			kexiaoendtime: "",
			jingshouren: event.jingshouren,
			baomingdate: event.baomingdate,
			jiezhidate: event.jiezhidate,
			kechengshu: parseInt(event.kechengshu),
			money1: event.money1,
			yihexiaoshu: event.yihexiaoshu,
			weixiaokeshi: event.kechengshu-event.yihexiaoshu,
			shoufeifangshi: event.shoufeifangshi,
			id: event.id,
			xiaoqu: event.xiaoqu,
			kecheng: event.kecheng,
			zhuangtai: 1,
			isxufei: 0,
			yikefeiyong: event.yikefeiyong,
			kechengid: event.kechengid,
			riqi: event.time,
			beizhu: event.beizhu
		})
		const res1 = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.time,
			jingshouren: event.jingshouren,
			xiaoqu: event.xiaoqu,
			leibie: "报课程",
		})
	} else if (event.leixing == 2) { //获取某个报课内容信息
		const dbJQL = uniCloud.databaseForJQL({
			event,
			context
		})
		dbJQL.setUser({ // 指定后续执行操作的用户信息，此虚拟用户将同时拥有传入的uid、role、permission
			// uid: 'user-id', // 用户id
			role: ['admin'], // 指定当前执行用户的角色为admin。如果只希望指定为admin身份，可以删除uid和permission字段
			// permission: [] // 用户权限
		})
		const order = await dbJQL.collection('baokelist').getTemp() // 临时表field方法内需要包含关联字段，否则无法建立关联关系
		const book = await dbJQL.collection('uni-id-users').field(
				'_id,name,age,nianji,school,mobile,avatar,zhuzhi')
		.getTemp() // 临时表field方法内需要包含关联字段，否则无法建立关联关系
		const res = await dbJQL.collection(order, book).where({
			_id: event._id
		}).get() // 注意collection方法内需要传入所有用到的表名，用逗号分隔，主表需要放在第一位
		return res
	} else if (event.leixing == 3) {
		const panduan = await db.collection('baokelist').doc(event.xufeifrom).get()
		if(panduan.isxufei){
			return '111'
		}else{
			const res = await db.collection('baokelist').add({
				jingshouren: event.jingshouren,
				baomingdate: event.baomingdate,
				jiezhidate: event.jiezhidate,
				kechengshu: parseInt(event.kechengshu),
				money1: event.money1,
				yihexiaoshu: event.yihexiaoshu,
				weixiaokeshi: event.kechengshu-event.yihexiaoshu,
				shoufeifangshi: event.shoufeifangshi,
				id: event.id,
				xiaoqu: event.xiaoqu,
				kecheng: event.kecheng,
				zhuangtai: event.zhuangtai,
				xufeifrom: event.xufeifrom,
				isxufei: 0,
				riqi: event.riqi,
				yikefeiyong: event.yikefeiyong,
				kexiaoendtime: "",
				kechengid: event.kechengid
			})
			const res1 = await db.collection('zhangwuliushui').add({
				jine: event.money1,
				biandong: "收入",
				leibie: event.leibie,
				xiaoqu: event.xiaoqu,
				detail: event.detail,
				riqi: event.riqi,
				jingshouren: event.jingshouren,
			})
			const res2 = await db.collection('baokelist').doc(event.xufeifrom).update({
				isxufei: 1,
			})
			const res3 = await db.collection('xiugaishuju').add({
				detail: event.detail,
				riqi: event.riqi,
				jingshouren: event.jingshouren,
				xiaoqu: event.xiaoqu,
				leibie: event.leibie,
			})
			return res
		}	
		
	} else if (event.leixing == 4) {
		const res = await db.collection('baokelist').add({
			xufeifrom: "",
			kexiaoendtime: "",
			jingshouren: event.jingshouren,
			baomingdate: event.baomingdate,
			jiezhidate: event.jiezhidate,
			kechengshu: parseInt(event.kechengshu),
			money1: event.money1,
			yihexiaoshu: event.yihexiaoshu,
			weixiaokeshi: event.kechengshu-event.yihexiaoshu,
			shoufeifangshi: event.shoufeifangshi,
			id: event.id,
			yikefeiyong: event.yikefeiyong,
			xiaoqu: event.xiaoqu,
			kecheng: event.kecheng,
			zhuangtai: event.zhuangtai,
			kechengid: event.kechengid,
			isxufei: 0,
			riqi: event.riqi
		})
		const res1 = await db.collection('zhangwuliushui').add({
			jine: event.money1,
			biandong: "收入",
			leibie: event.leibie,
			detail: event.detail,
			riqi: event.riqi,
			xiaoqu: event.xiaoqu,
			jingshouren: event.jingshouren,
		})
		const res2 = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			xiaoqu: event.xiaoqu,
			leibie: event.leibie,
		})
		return res
	} else if (event.leixing == 5) {
		const res = await db.collection('xiaokelist').doc(event.id).remove()
		const res0 = await db.collection('xiaokelist').where({
			baokeid:event.baokeid,
			yihexiaoshu:dbCmd.gt(event.yihexiaoshu)
		}).update({
			yihexiaoshu:dbCmd.inc(-1)
		})
		
		const res1 = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			xiaoqu: event.xiaoqu,
			leibie: "删除销课记录"
		})
		const res2 = await db.collection('baokelist').where({
			_id: event.baokeid
		}).update({
			yihexiaoshu: dbCmd.inc(-1),
			weixiaokeshi:dbCmd.inc(1),
			zhuangtai:1
		})
		return res
	} else if (event.leixing == 6) {
		const res = await db.collection('baokelist').doc(event._id).update({
			jingshouren: event.jingshouren,
			xiaoqu: event.xiaoqu,
			kecheng: event.kecheng,
			kechengid: event.kechengid,
			riqi: event.riqi
		})
		const res2 = await db.collection('xiugaishuju').add({
			detail: event.detail,
			riqi: event.riqi,
			jingshouren: event.jingshouren,
			xiaoqu: event.xiaoqu,
			leibie: event.leibie,
		})
		return res
	}
};